﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using StateGrid.MDBservice;
using System.IO;
using iTextSharp.text.pdf;

namespace StateGrid.ZGCG
{
    class ZGCG_MDB : MDBservice.MDBservice
    {
        public override String id
        {
            get
            {
                return "zhongguochengguo";
            }
        }

        private ZGCG_DAO zgcgDAO = new ZGCG_DAO();

        public override void clear()
        {
            base.clear();
        }

        public override String getTest()
        {
            IList<ZGCG_FL> r = zgcgDAO.GetAllZGCG_FL();
            String ret = String.Empty;
            foreach (ZGCG_FL i in r)
            {
                ret += i.ToString();
            }
            return ret;
        }

        public ZGCG_MDB()
        {
            pmTableName = "ZGCG_WF";
            formatPmTableName = "ZGCG";
            title = "中国成果";
        }

        public override void setColumnList()
        {
            this.ColumnList = new List<Column>();
            if (hasFormatTable)
            {
                this.ColumnList.Add(new Column("分类编码", OleDbType.WChar, 100));
                this.ColumnList.Add(new Column("成果编号", OleDbType.WChar, 20));
                this.ColumnList.Add(new Column("成果名称", OleDbType.WChar, 150));
                this.ColumnList.Add(new Column("完成单位", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("完成人", OleDbType.WChar, 100));
                this.ColumnList.Add(new Column("单位地址", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("邮政编码", OleDbType.WChar, 6));
                this.ColumnList.Add(new Column("单位电话", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("成果简介", OleDbType.WChar, 0));
                this.ColumnList.Add(new Column("中文主题词", OleDbType.WChar, 100));
                this.ColumnList.Add(new Column("研究起止时间", OleDbType.WChar, 50));
                this.ColumnList.Add(new Column("国图分类", OleDbType.WChar, 35));
                this.ColumnList.Add(new Column("成果类别", OleDbType.WChar, 15));
                this.ColumnList.Add(new Column("成果水平", OleDbType.WChar, 10));
                this.ColumnList.Add(new Column("鉴定部门", OleDbType.WChar, 50));
                this.ColumnList.Add(new Column("鉴定日期", OleDbType.WChar, 20));
                this.ColumnList.Add(new Column("立项或获奖", OleDbType.WChar, 150));
                this.ColumnList.Add(new Column("发布单位", OleDbType.WChar, 100));
                this.ColumnList.Add(new Column("资料来源", OleDbType.WChar, 60));
                this.ColumnList.Add(new Column("发布年份", OleDbType.WChar, 10));
                this.ColumnList.Add(new Column("来源编号", OleDbType.WChar, 10));
                this.ColumnList.Add(new Column("行业代码", OleDbType.WChar, 7));
                this.ColumnList.Add(new Column("行业名称", OleDbType.WChar, 50));
                this.ColumnList.Add(new Column("专利授权", OleDbType.WChar, 100));
                this.ColumnList.Add(new Column("开放", OleDbType.Boolean));
                this.ColumnList.Add(new Column("更新日期", OleDbType.Date));
            }
            else
            {
                //未支持
            }
        }

        protected override PM getPM(OleDbDataReader reader, out String err)
        {
            err = String.Empty;
            ZGCG_PM pm = new ZGCG_PM();
            if (hasFormatTable)
            {
                pm.flbm = reader["分类编码"].ToString().Trim();
                pm.cgmc = reader["成果名称"].ToString().Trim();
                pm.cgbh = reader["成果编号"].ToString().Trim();
                pm.wcr = reader["完成人"].ToString().Trim();
                pm.wcdw = reader["完成单位"].ToString().Trim();
                pm.dwdz = reader["单位地址"].ToString().Trim();
                pm.yzbm = reader["邮政编码"].ToString().Trim();
                pm.dwdh = reader["单位电话"].ToString().Trim();
                pm.cgjj = reader["成果简介"].ToString().Trim();
                pm.zwztc = reader["中文主题词"].ToString().Trim();
                pm.yjqzsj = reader["研究起止时间"].ToString().Trim();
                pm.gtfl = reader["国图分类"].ToString().Trim();
                pm.cglb = reader["成果类别"].ToString().Trim();
                pm.cgsp = reader["成果水平"].ToString().Trim();
                pm.jdbm = reader["鉴定部门"].ToString().Trim();
                pm.jdrq = reader["鉴定日期"].ToString().Trim();
                pm.lxhhj = reader["立项或获奖"].ToString().Trim();
                pm.fbdw = reader["发布单位"].ToString().Trim();
                pm.zlly = reader["资料来源"].ToString().Trim();
                try
                {
                    if (String.Empty.Equals(reader["发布年份"].ToString().Trim()))
                    {
                        //err += pm.flbm + "-" + pm.cgbh + ":" + pm.cgmc + ":发布年份为空\n";
                        pm.fbnf = 0;
                    }
                    else
                        pm.fbnf = int.Parse(reader["发布年份"].ToString().Trim());
                }
                catch
                {
                    err += pm.flbm + ":" + pm.cgbh + ":" + pm.cgmc + ": 发布年份\"" + reader["发布年份"] + "\"出错\n";
                }
                pm.lybh = reader["来源编号"].ToString().Trim();
                pm.hydm = reader["行业代码"].ToString().Trim();
                pm.hymc = reader["行业名称"].ToString().Trim();
                pm.zlsq = reader["专利授权"].ToString().Trim();
                try
                {
                    pm.kf = Boolean.Parse(reader["开放"].ToString().Trim());
                }
                catch (Exception)
                {
                    pm.kf = false;
                }
            }
            else
            {
                //不支持
            }
            pm.dlr = user.name;
            pm.fillEmptyStringAsNull();
            return pm;
        }

        public override bool checkPMValid(PM pm, out string errMsg)
        {
            errMsg = String.Empty;
            ZGCG_PM zgcg = (ZGCG_PM)pm;
            this.openOleDbConnection();
            sqlcmd.CommandText = @"select * from " + formatPmTableName + " where 成果编号=@成果编号 and 成果名称=@成果名称";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("成果编号", zgcg.cgbh));
            sqlcmd.Parameters.Add(new OleDbParameter("成果名称", zgcg.cgmc));
            OleDbDataReader reader = sqlcmd.ExecuteReader();

            if (reader.Read())
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return true;
            }
            else
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return false;
            }
            //            this.closeOleDbConnection();
        }

        public override string formatData(PM pm)
        {
            //反馈信息
            String result = String.Empty;
            String Empty = "NULL";
            ZGCG_PM zgcg = (ZGCG_PM)pm;

            zgcg.flbm = GlobalMethod.Semicolon(zgcg.flbm);
            zgcg.wcr = GlobalMethod.Semicolon(zgcg.wcr);
            zgcg.zwztc = GlobalMethod.Semicolon(zgcg.zwztc);
            zgcg.gtfl = GlobalMethod.Semicolon(zgcg.gtfl);
            zgcg.hydm = GlobalMethod.Semicolon(zgcg.hydm);
            zgcg.hymc = GlobalMethod.Semicolon(zgcg.hymc);
            zgcg.dlr = user.name;
            zgcg.gxrq = DateTime.Now;
            //分类编码可能有多个，以分类隔开
            String[] flbms = zgcg.flbm.Split(';');
            foreach (string flbm in flbms)
            {
                if (zgcgDAO.GetZGCG_FLByFlbm(flbm) == null)
                {
                    result += "分类编码" + flbm + "不存在 ";
                }
            }
            if (Empty.Equals(zgcg.cgmc))
            {
                result += "成果名称为空 ";
            }

            if (!result.Equals(String.Empty))
            {
                result += " : " + zgcg.cgbh + "《" + zgcg.cgmc + "》\n";
            }

            return result;
        }

        public override String insertPM(PM pm)
        {
            ZGCG_PM zgcg = (ZGCG_PM)pm;
            int back;
            String err = String.Empty;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(分类编码 char(100), 成果编号 char(20), 成果名称 char(150), 完成单位 char(255), 完成人 char(100), 单位地址 char(255), 邮政编码 char(6), 单位电话 char(255), 成果简介 text, 中文主题词 char(100), 研究起止时间 char(50), 国图分类 char(35), 成果类别 char(15), 成果水平 char(10), 鉴定部门 char(50), 鉴定日期 char(20), 立项或获奖 char(150), 发布单位 char(100), 资料来源 char(60), 发布年份 char(10), 来源编号 char(10), 行业代码 char(7), 行业名称 char(50), 专利授权 char(100), 开放 boolean, 更新日期 datetime)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return err;
                }
            }

            sqlcmd.CommandText = @"insert into " + formatPmTableName + @"(分类编码,成果编号,成果名称,完成单位,完成人,单位地址,邮政编码,单位电话,成果简介,中文主题词,研究起止时间,国图分类,成果类别,成果水平,鉴定部门,鉴定日期,立项或获奖,发布单位,资料来源,发布年份,来源编号,行业代码,行业名称,专利授权,开放,更新日期) values(@分类编码,@成果编号,@成果名称,@完成单位,@完成人,@单位地址,@邮政编码,@单位电话,@成果简介,@中文主题词,@研究起止时间,@国图分类,@成果类别,@成果水平,@鉴定部门,@鉴定日期,@立项或获奖,@发布单位,@资料来源,@发布年份,@来源编号,@行业代码,@行业名称,@专利授权,@开放,@更新日期)";
            sqlcmd.Connection = this.ObjConnection;
            OleDbParameter parameter;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", zgcg.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("成果编号", zgcg.cgbh));
            sqlcmd.Parameters.Add(new OleDbParameter("成果名称", zgcg.cgmc));
            sqlcmd.Parameters.Add(new OleDbParameter("完成单位", zgcg.wcdw));
            sqlcmd.Parameters.Add(new OleDbParameter("完成人", zgcg.wcr));
            sqlcmd.Parameters.Add(new OleDbParameter("单位地址", zgcg.dwdz));
            sqlcmd.Parameters.Add(new OleDbParameter("邮政编码", zgcg.yzbm));
            sqlcmd.Parameters.Add(new OleDbParameter("单位电话", zgcg.dwdh));
            sqlcmd.Parameters.Add(new OleDbParameter("成果简介", zgcg.cgjj));
            sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zgcg.zwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("研究起止时间", zgcg.yjqzsj));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zgcg.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("成果类别", zgcg.cglb));
            sqlcmd.Parameters.Add(new OleDbParameter("成果水平", zgcg.cgsp));
            sqlcmd.Parameters.Add(new OleDbParameter("鉴定部门", zgcg.jdbm));
            sqlcmd.Parameters.Add(new OleDbParameter("鉴定日期", zgcg.jdrq));
            sqlcmd.Parameters.Add(new OleDbParameter("立项或获奖", zgcg.lxhhj));
            sqlcmd.Parameters.Add(new OleDbParameter("发布单位", zgcg.fbdw));
            sqlcmd.Parameters.Add(new OleDbParameter("资料来源", zgcg.zlly));
            sqlcmd.Parameters.Add(new OleDbParameter("发布年份", zgcg.fbnf));
            sqlcmd.Parameters.Add(new OleDbParameter("来源编号", zgcg.lybh));
            sqlcmd.Parameters.Add(new OleDbParameter("行业代码", zgcg.hydm));
            sqlcmd.Parameters.Add(new OleDbParameter("行业名称", zgcg.hymc));
            sqlcmd.Parameters.Add(new OleDbParameter("专利授权", zgcg.zlsq));
            //sqlcmd.Parameters.Add(new OleDbParameter("开放", zgcg.kf));
            parameter = new OleDbParameter("开放", OleDbType.Boolean);
            parameter.Value = zgcg.kf;
            sqlcmd.Parameters.Add(parameter);
            parameter = new OleDbParameter("更新日期", OleDbType.DBDate);
            parameter.Value = zgcg.gxrq;
            sqlcmd.Parameters.Add(parameter);
            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();

            this.closeOleDbConnection();
            return err;
        }

        public override int deletePM(PM pm, out String err)
        {
            int back = 0;
            err = String.Empty;
            ZGCG_PM zgcg = (ZGCG_PM)pm;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(分类编码 char(100), 成果编号 char(20), 成果名称 char(150), 完成单位 char(255), 完成人 char(100), 单位地址 char(255), 邮政编码 char(6), 单位电话 char(255), 成果简介 text, 中文主题词 char(100), 研究起止时间 char(50), 国图分类 char(35), 成果类别 char(15), 成果水平 char(10), 鉴定部门 char(50), 鉴定日期 char(20), 立项或获奖 char(150), 发布单位 char(100), 资料来源 char(60), 发布年份 char(10), 来源编号 char(10), 行业代码 char(7), 行业名称 char(50), 专利授权 char(100), 开放 boolean, 更新日期 datetime)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return 0;
                }
            }
            //sql语句
            sqlcmd.CommandText = @"delete from " + formatPmTableName + " where 成果编号=@成果编号 and 成果名称=@成果名称 and 完成单位=@完成单位 and 发布年份=@发布年份";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("成果编号", zgcg.cgbh));
            sqlcmd.Parameters.Add(new OleDbParameter("成果名称", zgcg.cgmc));
            sqlcmd.Parameters.Add(new OleDbParameter("完成单位", zgcg.wcdw));
            sqlcmd.Parameters.Add(new OleDbParameter("发布年份", zgcg.fbnf));

            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = "Exception: " + e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();
            //            this.closeOleDbConnection();
            return back;
        }
        /* 现在的方法先delete再insert，就不用为每文库写update了
        public override bool updatePM(PM pm)
        {
            ZWQK_PM zwqk = (ZWQK_PM)pm;
            //打开连接
            this.openOleDbConnection();

            //sql语句
            OleDbCommand sqlcmd = new OleDbCommand(@"update ZWQK set 作者=@作者,作者单位=@作者单位,中文主题词=@中文主题词,外文主题词=@外文主题词,国图分类=@国图分类 where 中文篇名=@中文篇名 and 文件名=@文件名", this.ObjConnection);

            sqlcmd.Parameters.Add(new OleDbParameter("作者", zwqk.zz));
            sqlcmd.Parameters.Add(new OleDbParameter("作者单位", zwqk.zzdw));
            sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zwqk.zwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("外文主题词", zwqk.wwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zwqk.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwqk.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("文件名", zwqk.wjm));
            int back = sqlcmd.ExecuteNonQuery();
            if (back == 0)
            {
                return false;
            }
            //objConnection.Close();
            return true;
        }*/

        public override bool findServerDuplicate(PM pm)
        {
            ZGCG_PM zgcg = (ZGCG_PM)pm;
            ZGCG_PM result = zgcgDAO.GetZGCG_PMByCgmcAndFbnf(zgcg.cgmc, zgcg.fbnf);
            if (result != null)
                return true;
            else
                return false;
        }

        public override String insertPMwithUpdateWJM(PM pm)
        {
            String err = String.Empty;
            ZGCG_PM zgcg = (ZGCG_PM)pm;
            try
            {
                zgcgDAO.InsertZGCG_PM(zgcg);
            }
            catch (Exception e)
            {
                err += e.Message + "\n";
            }
            return err;
        }

        public override string addOneRecord(PM pm)
        {
            String err = String.Empty;
            ZGCG_PM zgcg = (ZGCG_PM)pm;
            zgcg.gxrq = DateTime.Now;
            zgcg.eraseNullInEmptyString();
            try
            {
                zgcgDAO.InsertZGCG_PM(zgcg);
            }
            catch (Exception e)
            {
                err += e.Message + "\n";
            }
            return err;
        }

    }
}
